package edu.yzu.testspringboot002.config;


import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 *
 * @author yulang
 * @date 2023-08-11
 */

@Configuration
@Slf4j
public class RedissonConfig {

    private static String REDISSON_LINK_ADDRESS  ="redis://%s:%d";


    /**
     * redisson客户
     *  单机版本
     * @param redisProperties 复述,属性
     * @return {@link RedissonClient }	返回值类型
     * @author 余浪
     */
    @Bean
    @ConditionalOnBean(RedisProperties.class)
    public RedissonClient redissonClient(RedisProperties redisProperties){
        Config config = new Config();
        config.useSingleServer()
                .setConnectTimeout(5000)
                .setDatabase(redisProperties.getDatabase())
                .setClientName("EDWIN-REDISSON")
                .setAddress(String.format(REDISSON_LINK_ADDRESS,redisProperties.getHost(),redisProperties.getPort()));
        return  Redisson.create(config);
    }
}
